namespace SubSonic
{
    using SubSonic.Utilities;
    using System;
    using System.Data;
    using System.Web.UI.WebControls;

    public class ManyManyList : CheckBoxList
    {
        private string foreignTableName = string.Empty;
        private string foreignTextField = string.Empty;
        private string mapTableName = string.Empty;
        private string primaryKeyValue = "0";
        private string primaryTableName = string.Empty;
        private string providerName = string.Empty;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            if (this.Items.Count == 0)
            {
                if ((string.IsNullOrEmpty(this.primaryKeyValue) || string.IsNullOrEmpty(this.primaryTableName)) || string.IsNullOrEmpty(this.mapTableName))
                {
                    throw new Exception("Missing a setting. Please be sure to set the PrimaryKeyValue (e.g. 'ProductID', PrimaryTableName (e.g. 'Products'), and MapTableName (e.g. Product_Category_Map)");
                }
                DataProvider instance = DataService.GetInstance(this.providerName);
                TableSchema.Table table = DataService.GetSchema(this.foreignTableName, this.providerName, TableType.Table);
                TableSchema.Table table2 = DataService.GetSchema(this.primaryTableName, this.providerName, TableType.Table);
                string columnName = table.PrimaryKey.ColumnName;
                string foreignTextField = table.Columns[1].ColumnName;
                if (!string.IsNullOrEmpty(this.ForeignTextField))
                {
                    if (!table.Columns.Contains(this.ForeignTextField))
                    {
                        throw new Exception("Invalid ForeignTextField. Please be sure to set the value to a field name from " + this.foreignTableName);
                    }
                    foreignTextField = this.ForeignTextField;
                }
                string parameterName = Utility.PrefixParameter("id", instance);
                QueryCommand cmd = new QueryCommand("SELECT " + columnName + "," + foreignTextField + " FROM " + this.foreignTableName + ";", this.providerName);
                string commandSql = cmd.CommandSql;
                cmd.CommandSql = commandSql + "SELECT " + columnName + " FROM " + this.mapTableName + " WHERE " + table2.PrimaryKey.ColumnName + " = " + parameterName;
                cmd.Parameters.Add(parameterName, this.primaryKeyValue);
                using (IDataReader reader = DataService.GetReader(cmd))
                {
                    while (reader.Read())
                    {
                        ListItem item = new ListItem(reader[1].ToString(), reader[0].ToString());
                        this.Items.Add(item);
                    }
                    reader.NextResult();
                    while (reader.Read())
                    {
                        string stringB = reader[columnName].ToString().ToLower();
                        foreach (ListItem item2 in this.Items)
                        {
                            if (Utility.IsMatch(item2.Value, stringB))
                            {
                                item2.Selected = true;
                            }
                        }
                    }
                }
            }
        }

        public void Save()
        {
            QueryCommandCollection commands = new QueryCommandCollection();
            DataProvider instance = DataService.GetInstance(this.providerName);
            TableSchema.Table table = DataService.GetSchema(this.foreignTableName, this.providerName, TableType.Table);
            TableSchema.Table table2 = DataService.GetSchema(this.primaryTableName, this.providerName, TableType.Table);
            string columnName = table.PrimaryKey.ColumnName;
            string str2 = table2.PrimaryKey.ColumnName;
            string parameterName = Utility.PrefixParameter("id", instance);
            QueryCommand command = new QueryCommand("DELETE FROM " + this.mapTableName + " WHERE " + str2 + " = " + parameterName, this.providerName);
            command.AddParameter(parameterName, this.primaryKeyValue);
            commands.Add(command);
            string str4 = Utility.PrefixParameter("fkID", instance);
            string str5 = Utility.PrefixParameter("pkID", instance);
            foreach (ListItem item in this.Items)
            {
                if (item.Selected)
                {
                    QueryCommand command2 = new QueryCommand("INSERT INTO " + this.mapTableName + " (" + columnName + ", " + str2 + ") VALUES (" + str4 + "," + str5 + ")", this.providerName);
                    command2.Parameters.Add(str4, item.Value);
                    command2.Parameters.Add(str5, this.primaryKeyValue);
                    commands.Add(command2);
                }
            }
            DataService.ExecuteTransaction(commands);
        }

        public string ForeignTableName
        {
            get
            {
                return this.foreignTableName;
            }
            set
            {
                this.foreignTableName = value;
            }
        }

        public string ForeignTextField
        {
            get
            {
                return this.foreignTextField;
            }
            set
            {
                this.foreignTextField = value;
            }
        }

        public string MapTableName
        {
            get
            {
                return this.mapTableName;
            }
            set
            {
                this.mapTableName = value;
            }
        }

        public string PrimaryKeyValue
        {
            get
            {
                return this.primaryKeyValue;
            }
            set
            {
                this.primaryKeyValue = value;
            }
        }

        public string PrimaryTableName
        {
            get
            {
                return this.primaryTableName;
            }
            set
            {
                this.primaryTableName = value;
            }
        }

        public string ProviderName
        {
            get
            {
                if (string.IsNullOrEmpty(this.providerName))
                {
                    this.providerName = DataService.Provider.Name;
                }
                return this.providerName;
            }
            set
            {
                this.providerName = value;
            }
        }
    }
}

